﻿using System.Linq;
using Newtonsoft.Json;

namespace Edu.Http.Extensions
{
    public static class ObjectExtension
    {
        public static string ToQueryString(this object model)
        {
            var queryString = string.Empty;
            if (model is null)
            {
                return queryString;
            }

            var properties = model.GetType().GetProperties();

            queryString = string.Join("&", properties.Select(d => $"{d.Name}={d.GetValue(model)}"));
            return queryString;
        }
        public static string Serialize(this object obj)
        {
            var setting = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
                NullValueHandling=NullValueHandling.Ignore
            };
            var jsonStr = JsonConvert.SerializeObject(obj, Formatting.None, setting);
            return jsonStr;
        }
    }
}
